cmake_minimum_required(VERSION 3.16)
project(PythonQtEnvManager VERSION 1.0)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置全局UTF-8编码支持
if(MSVC)
    add_compile_options(/utf-8)
endif()

# 启用Qt自动工具
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 查找Qt5
find_package(Qt5 REQUIRED COMPONENTS Core Widgets)

# 查找Python
find_package(Python COMPONENTS Interpreter Development REQUIRED)

# 查找PyBind11
find_package(pybind11 QUIET)

if(NOT pybind11_FOUND)
    # 如果系统没有安装，则尝试使用third_party目录中的版本
    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/pybind11/CMakeLists.txt)
        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third_party/pybind11 pybind11_build)
    else()
        message(FATAL_ERROR "PyBind11 not found. Please install pybind11 or place it in third_party/pybind11")
    endif()
endif()

# 设置源文件
set(SOURCES
    src/core/ObjectRegistry.cpp
    src/core/ScriptManager.cpp
    src/core/PropertyAccessor.cpp
    src/core/PythonBindServer.cpp
    src/core/ServerManager.cpp
    src/exceptions/PythonEnvException.cpp
    src/exceptions/ObjectNotFoundException.cpp
    src/exceptions/ScriptExecutionException.cpp
    src/utils/TypeConverter.cpp
    src/utils/StringUtils.cpp
)

set(HEADERS
    src/core/ObjectRegistry.h
    src/core/ScriptManager.h
    src/core/PropertyAccessor.h
    src/core/PythonBindServer.h
    src/core/ServerManager.h
    server/IPythonBindServer.h
    src/exceptions/PythonEnvException.h
    src/exceptions/ObjectNotFoundException.h
    src/exceptions/ScriptExecutionException.h
    src/utils/TypeConverter.h
    src/utils/StringUtils.h
    src/include/PythonQtEnvManager.h
)

# 创建库
add_library(PythonQtEnvManager ${SOURCES} ${HEADERS})

# 链接必要的库
target_link_libraries(PythonQtEnvManager 
    Qt5::Core
    Qt5::Widgets
    Python::Python
)

# 如果使用的是子模块版本的pybind11
if(TARGET pybind11::embed)
    target_link_libraries(PythonQtEnvManager pybind11::embed)
else()
    # 如果使用系统安装的pybind11
    target_link_libraries(PythonQtEnvManager pybind11::embed)
endif()

# 包含头文件目录
target_include_directories(PythonQtEnvManager PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/src/include
    ${CMAKE_CURRENT_SOURCE_DIR}/src/core
    ${CMAKE_CURRENT_SOURCE_DIR}/server
    ${CMAKE_CURRENT_SOURCE_DIR}/src/exceptions
    ${CMAKE_CURRENT_SOURCE_DIR}/src/utils
)

# 包含PyBind11头文件
target_include_directories(PythonQtEnvManager PRIVATE 
    ${CMAKE_CURRENT_SOURCE_DIR}/third_party/pybind11/include
)

# 为MSVC添加额外的定义
if(MSVC)
    target_compile_definitions(PythonQtEnvManager PRIVATE
        PYBIND11_COMPILER_TYPE="_MSC_VER"
        PYBIND11_STDLIB="_MSVC_STDLIB"
        PYBIND11_BUILD_ABI="_MSC_VER"
        HAVE_SNPRINTF
        PY_SSIZE_T_CLEAN
    )
endif()

# 创建示例可执行文件
add_executable(example examples/basic_usage/main.cpp)
target_link_libraries(example PythonQtEnvManager)

# 将Python脚本文件拷贝到运行目录
configure_file(examples/basic_usage/example_script.py ${CMAKE_CURRENT_BINARY_DIR}/example_script.py COPYONLY)
configure_file(examples/basic_usage/button_script.py ${CMAKE_CURRENT_BINARY_DIR}/button_script.py COPYONLY)

# 在Windows上，还需要将脚本文件拷贝到可执行文件目录
if(WIN32)
    # 获取可执行文件的输出目录
    get_target_property(EXECUTABLE_OUTPUT_PATH example RUNTIME_OUTPUT_DIRECTORY)
    if(NOT EXECUTABLE_OUTPUT_PATH)
        set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
    endif()
    
    # 添加自定义命令，将脚本文件拷贝到可执行文件目录
    add_custom_command(TARGET example POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/examples/basic_usage/example_script.py
        $<TARGET_FILE_DIR:example>/example_script.py
    )
    
    add_custom_command(TARGET example POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        ${CMAKE_CURRENT_SOURCE_DIR}/examples/basic_usage/button_script.py
        $<TARGET_FILE_DIR:example>/button_script.py
    )
endif()

# 安装规则
install(TARGETS PythonQtEnvManager
    EXPORT PythonQtEnvManagerTargets
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)

install(DIRECTORY src/include/ DESTINATION include)
install(FILES README.md DESTINATION share/doc/PythonQtEnvManager)
# 安装Python脚本文件
install(FILES examples/basic_usage/example_script.py DESTINATION bin)
install(FILES examples/basic_usage/button_script.py DESTINATION bin)